package com.example.lenovo.test1;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

public class MainActivity extends AppCompatActivity {
    private PopupWindow mPopupWindow;
    private float mX;
    private float mY;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        LayoutInflater inflater = LayoutInflater.from(this);
        View popView = inflater.inflate(R.layout.popup_content_layout, null);
        mPopupWindow = new PopupWindow(popView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        mPopupWindow.setBackgroundDrawable(new ColorDrawable(0));
        ImageView imageView = popView.findViewById(R.id.btn_pop_close);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPopupWindow.dismiss();
            }
        });
        Button btnShow = findViewById(R.id.btnShow);
        btnShow.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                mX = event.getRawX();
                mY = event.getRawY()+50;
                return false;
            }
        });
    }
    protected void onBtnOnClick(View view){
        if (mPopupWindow.isShowing()) {
        mPopupWindow.showAtLocation(view, 0, (int)mX, (int) mY); //设置popwindow显示位置
        mPopupWindow.setFocusable(true); //获取popwindow焦点
        mPopupWindow.setOutsideTouchable(true);  //设置popwindow如果点击外面区域，便关闭。
        mPopupWindow.update();
        }
    }
}